<template>
  <div v-if="visible" class="video-overlay" @click="handleOverlayClick">
    <div class="video-container" @click.stop>
      <!-- 关闭按钮 -->
      <button class="close-btn" @click="close">
        <img src="@/assets/icons/close.png" alt="关闭" class="close-icon" />
      </button>
      
      <!-- 视频播放器 -->
      <video 
        ref="videoRef" 
        class="video-player" 
        :src="videoUrl" 
        controls 
        controlsList="nodownload"
        :autoplay="autoplay"
        :loop="loop"
        :muted="muted"
        playsinline
        webkit-playsinline
        @loadedmetadata="onVideoLoaded"
        @error="onVideoError"
      >
        您的浏览器不支持视频播放。
      </video>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, watch } from 'vue'

interface Props {
  visible: boolean
  videoUrl: string
  autoplay?: boolean
  loop?: boolean
  muted?: boolean
}

interface Emits {
  (e: 'update:visible', value: boolean): void
  (e: 'video-loaded'): void
  (e: 'video-error', error: Event): void
}

const props = withDefaults(defineProps<Props>(), {
  autoplay: false,
  loop: false,
  muted: false
})

const emit = defineEmits<Emits>()

const videoRef = ref<HTMLVideoElement | null>(null)

const close = () => {
  // 暂停视频
  if (videoRef.value) {
    videoRef.value.pause()
    videoRef.value.currentTime = 0
  }
  emit('update:visible', false)
}

// 暴露 close 方法和视频元素引用给父组件
defineExpose({
  close,
  videoElement: videoRef
})

const handleOverlayClick = () => {
}

const onVideoLoaded = () => {
  emit('video-loaded')
}

const onVideoError = (error: Event) => {
  emit('video-error', error)
}

// 监听 visible 变化，自动播放视频
watch(() => props.visible, (newVal) => {
  if (newVal && props.autoplay && videoRef.value) {
    videoRef.value.play().catch(err => {
      console.error('视频自动播放失败:', err)
    })
  }
})
</script>

<style scoped>
.video-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.85);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  animation: fadeIn 0.3s ease-out;
}

.video-container {
  position: relative;
  width: 90%;
  max-width: 750px;
  background-color: #000;
  border-radius: 12px;
  overflow: hidden;
  animation: scaleIn 0.3s ease-out;
}

.close-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  border: none;
  width: 60px;
  height: 60px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  z-index: 10;
  transition: background-color 0.2s ease;
}

.close-btn:hover {
  background-color: rgba(0, 0, 0, 0.7);
}

.close-btn:active {
  transform: scale(0.95);
}

.close-icon {
  width: 40px;
  height: 40px;
  object-fit: contain;
  filter: invert(1); /* 将图标变为白色 */
}

.video-player {
  width: 100%;
  height: auto;
  display: block;
  max-height: 80vh;
  object-fit: contain;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes scaleIn {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

/* 响应式设计 */
@media (max-width: 480px) {
  .video-container {
    width: 95%;
    border-radius: 8px;
  }
  
  .close-btn {
    width: 48px;
    height: 48px;
    top: 12px;
    right: 12px;
  }
  
  .close-icon {
    width: 32px;
    height: 32px;
  }
}
</style>

